
package com.lzx.iteam.util;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.widget.Toast;
import com.lzx.iteam.R;

public class ShakeListener implements SensorEventListener {

    private static final String TAG = "CloudDialerActivity";

//    private static final int FORCE_THRESHOLD = 350;

    private static final int TIME_THRESHOLD = 100;

//    private static final int SHAKE_TIMEOUT = 500;

//    private static final int SHAKE_DURATION = 1000;

//    private static final int SHAKE_COUNT = 1;

    public SensorManager mSensorMgr;

    private float mLastX = -1.0f, mLastY = -1.0f, mLastZ = -1.0f;

    private long mLastTime;

    private OnShakeListener mShakeListener;

    private Context mContext;

//    private int mShakeCount = 0;

//    private long mLastShake;

//    private long mLastForce;

    float mAxisX;
    float mAxisY;
    float mAxisZ;
    private Sensor mAccSensor;
    
    public float getAxisX() {
        return mAxisX;
    }
    public interface OnShakeListener {
        public void onShake(float axisX, float axisY, float axisZ);
        // public void onShakeHorizontal();
        // public void onShakeVertical();
    }

    public ShakeListener(Context context) {
        mContext = context;
        mSensorMgr = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
        
        if (mSensorMgr == null) {
            // throw new UnsupportedOperationException("Sensors not supported");
            Toast.makeText(mContext, R.string.err_acc_sensor, Toast.LENGTH_LONG).show();
        }
        
        mAccSensor = mSensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        resume();
    }

    public void setOnShakeListener(OnShakeListener listener) {
        mShakeListener = listener;
    }

    public void resume() {
        boolean supported = mSensorMgr.registerListener(this, mAccSensor, SensorManager.SENSOR_DELAY_UI);
        if (!supported) {//not fatal exception
           // mSensorMgr.unregisterListener(this);
            // throw new UnsupportedOperationException("Accelerometer not supported");
            //Toast.makeText(mContext, R.string.err_acc_sensor+"hehe", Toast.LENGTH_LONG).show();
        }
    }

    public void pause() {
        if (mSensorMgr != null) {
            mSensorMgr.unregisterListener(this);
        }
    }

    public void onSensorChanged(SensorEvent event) {

        if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
            return;
        }

        long now = System.currentTimeMillis();

        /*if ((now - mLastForce) > SHAKE_TIMEOUT) {
            mShakeCount = 0;
        }*/

        if ((now - mLastTime) > TIME_THRESHOLD) {
//            long diff = now - mLastTime;
           
            // This timestep's delta rotation to be multiplied by the current
            // rotation
//            float timestamp;

            if (now != 0) {
                // Axis of the rotation sample, not normalized yet.
                float axisX = event.values[0];
                float axisY = event.values[1];
                float axisZ = event.values[2];
                
                axisX /= 9.8;
                axisY /= 9.8;
                axisZ /= 9.8;
                
                if (((axisX > 0.6) || (axisX < -0.6)))  {
                    if (mShakeListener != null) {
                        mShakeListener.onShake(axisX, axisY, axisZ);
                    }
                }

            }
//            timestamp = event.timestamp;
            
            mLastTime = now;
            mLastX = event.values[SensorManager.DATA_X];
            mLastY = event.values[SensorManager.DATA_Y];
            mLastZ = event.values[SensorManager.DATA_Z];
        }
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {


    }
}
